package com.example.market_management.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.market_management.mapper.orderMapper;
import com.example.market_management.mapper.productMapper;
import com.example.market_management.mapper.userMapper;
import com.example.market_management.model.Order;
import com.example.market_management.model.Product;
import com.example.market_management.model.User;
import com.example.market_management.model.vo.CommonResult;

import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;


import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("order")
@CrossOrigin
@Slf4j
public class orderController {

    @Autowired
    private orderMapper  orderMapper;
    @Autowired
    private productMapper productMapper;
    @Autowired
    private userMapper   userMapper;


    /**
     *新增订单
     * @param customerId  顾客Id
     * @param productId   产品Id
     * @param orderNum    产品数量
     * @param deliveryAddress  配送地址
     * @return
     */
    @PostMapping("/addOrder")
    private CommonResult addOrder(int customerId,String productId,int orderNum,String deliveryAddress){
       Order order=new Order();
       order.setCustomerId(customerId);
       order.setProductId(productId);
       order.setOrderNum(orderNum);
       order.setDeliveryAddress(deliveryAddress);
       order.setOrderStatus(0);
       //当前时间为下单时间
       Date nowTime=new Date();
       order.setOrderTime(nowTime);
       //第二天10点为预计发货时间
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE,1);
        calendar.set(Calendar.HOUR_OF_DAY,10);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        Date startTime=calendar.getTime();
        order.setStartTime(startTime);
        //预计收货时间
        //获取当前时间的时间戳
        Long current=nowTime.getTime();
       Product product=productMapper.selectById(productId);
       long LockTime=product.getLockTime();
       long  total=current+LockTime*24*60*60*1000;
        Date deliveryTime = new Date(total);order.setDeliveryTime(deliveryTime);
       int i= orderMapper.insert(order);
       if(i==1)
           return CommonResult.success("添加订单成功");
       else
           return CommonResult.success("添加订单失败");
    }


    /**
     * 删除订单
     * @param orderId  订单Id
     * @return
     */
    @DeleteMapping("/delOrder")
    private CommonResult  delOrder(String orderId){
       int i= orderMapper.deleteById(orderId);
       if(i==1)
           return CommonResult.success("删除订单成功");
        else
            return CommonResult.success("删除订单失败");
    }

    /**
     * 查询订单信息  不输入订单状态则为全部订单信息
     * @param userId
     * @param orderStatus
     * @return
     */
    @GetMapping("/orderList")
    private CommonResult orderList(  Integer userId,Integer  orderStatus){
        List<Order> orders=orderMapper.orderList(userId,orderStatus);
        return CommonResult.success("查询当前用户订单成功:",orders);
    }


    /**
     * 查询订单数量  不传入orderStatus则为全部订单   0为已下单 1已发货  2为已收货
     * @param userId
     * @param orderStatus
     * @return
     */
    @GetMapping("/orderCount")
    private CommonResult orderCount(Integer userId, Integer  orderStatus){
       int i= orderMapper.orderCount(userId,orderStatus);
       return CommonResult.success("查询出的订个数如下:",i);
    }

    /**
     * 查询订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/orderInfo/{orderId}")
    private CommonResult orderInfo(@PathVariable String orderId){
         Order order=orderMapper.orderInfo(orderId);
         return CommonResult.success("查询订单详情成功",order);
    }

    /**
     * 支付密码确认
     * @param userId
     * @param payCode
     * @return
     */
    @GetMapping("/payCode/{userId}/{payCode}")
    private  CommonResult  payCode(Integer userId,String payCode){
        //将输入的支付密码加密再和数据库的密码进行匹配
        String encodePayCode= DigestUtils.md5DigestAsHex(payCode.getBytes());
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("pay_code",encodePayCode);
        User user =userMapper.selectOne(queryWrapper);
        if(user!=null) return CommonResult.success("支付密码确认成功",user);
        else  return CommonResult.failed("支付密码确认失败");
    }

    /**
     * 修改支付密码
     * @param userId
     * @param oldPayCode
     * @param newPayCode
     * @return
     */
    @PutMapping("/updatePayCode")
    private  CommonResult updatePayCode(Integer
                                                    userId, String oldPayCode, String  newPayCode){
        //先确认用户的原支付密码
        String encodeOldPayCode=DigestUtils.md5DigestAsHex(oldPayCode.getBytes());
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("pay_code",encodeOldPayCode);
        queryWrapper.eq("user_id",userId);
        User user=userMapper.selectOne(queryWrapper);
        if(user!=null){
         log.info("原支付密码正确");
         //将新密码加密
            String encodeNewPayCode=DigestUtils.md5DigestAsHex(newPayCode.getBytes());
         int i=orderMapper.updatePayCode(userId,encodeNewPayCode);
         if(i==1) return CommonResult.success("更新密码成功");
         else  return CommonResult.failed("更新密码失败");

        }
        else
            return  CommonResult.failed("原支付密码错误");
    }

    /**
     * 忘记支付密码
     * @param userId
     * @param userName
     * @param phoneNumber
     * @param newPayCode
     * @return
     */
    @GetMapping("/forgetPayCode")
    public CommonResult  forgetPayCode(Integer userId,String userName,String phoneNumber,String newPayCode){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("user_name",userName);
        queryWrapper.eq("phoneNumber",phoneNumber);
        User user=userMapper.selectOne(queryWrapper);
        if(user!=null){
            String encodeNewPayCode=DigestUtils.md5DigestAsHex(newPayCode.getBytes());
            user.setPayCode(encodeNewPayCode);
            userMapper.updateById(user);
            return CommonResult.success("修改密码成功");
        }
        else return CommonResult.failed("修改密码失败");
    }

}
